package com.rainman.noswagger.info_bean;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.rainman.noswagger.NoSwaggerCore;
import com.rainman.noswagger.core.BaseBean;
import lombok.Data;

import java.util.*;

@Data
public class ClassInfoBean extends BaseBean {

    private CompilationUnit cu;
    private TypeDeclaration<?> type;

    /**
     * 导包信息
     */
    private List<String> importsStrList;

    /**
     * 包名
     */
    private String packageName;

    /**
     * 接口路径
     */
    private String baseUrl;

    /**
     * 是否为controller
     */
    private Boolean controllerClass;

    /**
     * 类参数
     */
    private List<FiledInfoBean> filedInfoBeanList;

    /**
     * 接口
     * 是controller时此参数有值
     */
    private List<MethodInfoBean> methodInfoBeanList;

    public ClassInfoBean(CompilationUnit cu, TypeDeclaration<?> type, NoSwaggerCore core){
        super(type.getNameAsString(),core,type.getAnnotations(),cu.getComment());
        importsStrList=new ArrayList<>();
        filedInfoBeanList=new ArrayList<>();
        methodInfoBeanList =new ArrayList<>();

        this.cu=cu;
        this.type=type;
        controllerClass=false;
        Optional<PackageDeclaration> packageDeclaration = cu.getPackageDeclaration();
        if(packageDeclaration.isPresent()){

            packageName=packageDeclaration.get().getNameAsString();
        }


        NodeList<ImportDeclaration> importDeclarationNodeList = cu.getImports();

        for(ImportDeclaration importDeclaration:importDeclarationNodeList){
            importsStrList.add(importDeclaration.getNameAsString());
        }

        baseUrl = core.getCoreConfig().getClassInfoInterface().getBaseUrl(this);
        controllerClass=core.getCoreConfig().getClassInfoInterface().isControllerClass(this);

        //非controller要先初始化类参数 为后续做准备
        if(!controllerClass){

            initField();

        }

    }

    /**
     * 构建变量
     */
    private void initField() {
        List<FieldDeclaration> fields = type.getFields();
        for(FieldDeclaration fieldDeclaration:fields){
            NodeList<VariableDeclarator> variables = fieldDeclaration.getVariables();
            for(VariableDeclarator variableDeclarator:variables){

                filedInfoBeanList.add(new FiledInfoBean(fieldDeclaration,variableDeclarator,getCore(),this));
            }

        }
    }

    /**
     * 构建方法，与方法参数
     * 要在所有的类构建完变量后进行
     */
    public void buildController(){

        if(!controllerClass) {
            return ;
        }

        //初始化controller.method数据
        List<MethodDeclaration> methodDeclarations = type.getMethods();

        for(MethodDeclaration methodDeclaration:methodDeclarations){

            MethodInfoBean methodInfoBean = new MethodInfoBean(methodDeclaration, this, getCore());

            //先判断是否为接口，非接口不加入到methodInfoBeanList中
            if(methodInfoBean.getUrls().size()>0){
                methodInfoBeanList.add(methodInfoBean);
            }

        }

        return ;
    }

/*
    public JSONObject getInfo() {
        JSONObject reJson=new JSONObject();
        reJson.set("packageName",packageName);
        reJson.set("name",getName());
        reJson.set("baseUrl",baseUrl);
        reJson.set("controllerClass",controllerClass);
        JSONArray filedInfo=new JSONArray();
        for(FiledInfoBean filedInfoBean:filedInfoBeanList){
            filedInfo.add(filedInfoBean.getInfo());

        }
        reJson.set("filedInfo",filedInfo);
        for (String key : getOtherInfo().keySet()) {
            reJson.set(key,getOtherInfo().get(key));
        }
        return reJson;
    }*/
}
